package cn.crex1.xiaomi.core.orm.type.handler;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;

import cn.crex1.xiaomi.core.orm.type.converter.BigDecimalTypeConverter;
import cn.crex1.xiaomi.core.orm.type.converter.BigIntegerTypeConverter;
import cn.crex1.xiaomi.core.orm.type.converter.DateTypeConverter;
import cn.crex1.xiaomi.core.orm.type.converter.DoubleTypeConverter;
import cn.crex1.xiaomi.core.orm.type.converter.IntegerTypeConverter;
import cn.crex1.xiaomi.core.orm.type.converter.LongTypeConverter;
import cn.crex1.xiaomi.core.orm.type.converter.StringTypeConverter;

public class TypeHandlerImpl implements TypeHandler {

	private static String intclassType = "class java.lang.Integer";

	private static String longclassType = "class java.lang.Long";

	private static String floatclassType = "class java.lang.Float";

	private static String doubleclassType = "class java.lang.Double";

	private static String StringclassType = "class java.lang.String";

	private static String dateclassType = "class java.util.Date";

	private static String biclassType = "class java.math.BigInteger";

	private static String bdclassType = "class java.math.BigDecimal";

	/**
	 * 类型转换 obj1：目标类型 obj2：需要转换的目标对象
	 */
	@Override
	public Object converterTye(Object obj1, Object obj2) {
		if (obj2 != null) {
			if (obj2 instanceof Integer && intclassType.equals(obj1)) {
				IntegerTypeConverter iConverter = new IntegerTypeConverter();
				return iConverter.converterType(new StringTypeConverter().converterType(obj2));
			}
			if (obj2 instanceof Long && longclassType.equals(obj1)) {
				LongTypeConverter lConverter = new LongTypeConverter();
				return lConverter.converterType(new StringTypeConverter().converterType(obj2));
			}
			if (obj2 instanceof Float && floatclassType.equals(obj1)) {
				DoubleTypeConverter dConverter = new DoubleTypeConverter();
				return dConverter.converterFloatType(new StringTypeConverter().converterType(obj2));
			}
			if (obj2 instanceof Double && doubleclassType.equals(obj1)) {
				DoubleTypeConverter dConverter = new DoubleTypeConverter();
				return dConverter.converterDoubleType(new StringTypeConverter().converterType(obj2));
			}
			if (obj2 instanceof String && StringclassType.equals(obj1)) {
				StringTypeConverter sConverter = new StringTypeConverter();
				return sConverter.converterType(obj2);
			}
			if (obj2 instanceof Timestamp && dateclassType.equals(obj1)) {
				DateTypeConverter dConverter = new DateTypeConverter();
				return dConverter.converterType(new StringTypeConverter().converterType(obj2));
			}
			if (obj2 instanceof java.sql.Date && dateclassType.equals(obj1)) {
				DateTypeConverter dConverter = new DateTypeConverter();
				return dConverter.converterTypetoDate((java.sql.Date) obj2);
			}
			if (obj2 instanceof BigInteger && intclassType.equals(obj1)) {
				BigIntegerTypeConverter biConverter = new BigIntegerTypeConverter();
				return biConverter.converterTypetoInteger(
						biConverter.converterType(new StringTypeConverter().converterType(obj2)));
			}
			if (obj2 instanceof BigInteger && longclassType.equals(obj1)) {
				BigIntegerTypeConverter biConverter = new BigIntegerTypeConverter();
				return biConverter
						.converterTypetoLong(biConverter.converterType(new StringTypeConverter().converterType(obj2)));
			}
			if (obj2 instanceof BigInteger && biclassType.equals(obj1)) {
				BigIntegerTypeConverter biConverter = new BigIntegerTypeConverter();
				return biConverter
						.converterTypetoLong(biConverter.converterType(new StringTypeConverter().converterType(obj2)));
			}
			if (obj2 instanceof BigDecimal && doubleclassType.equals(obj1)) {
				BigDecimalTypeConverter bdConverter = new BigDecimalTypeConverter();
				return bdConverter.converterTypetoDouble(
						bdConverter.converterType(new StringTypeConverter().converterType(obj2)));
			}
			if (obj2 instanceof BigDecimal && bdclassType.equals(obj1)) {
				BigDecimalTypeConverter bdConverter = new BigDecimalTypeConverter();
				return bdConverter.converterTypetoDouble(
						bdConverter.converterType(new StringTypeConverter().converterType(obj2)));
			}
		}else {
			try {
				return wrapperDefaultValue(obj1);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	private <T> Object wrapperDefaultValue(T type) throws IllegalAccessException {
		if (intclassType.equals(type)) {
			return 0;
		}
		if (longclassType.equals(type) || biclassType.equals(type)) {
			return 0L;
		}
		if (floatclassType.equals(type) || doubleclassType.equals(type) || bdclassType.equals(type)) {
			return 0.00;
		}if (StringclassType.equals(type)) {
			return "";
		}if (dateclassType.equals(type)) {
			return LocalDateTime.now();
		}else {
			return null;
		}
	}

}
